//
//  工程名称:XJL_WeChat
//  文件名称:XJLContactsViewController.m
//
//  创建者  :Created by 许佳莉
//  创建时间:on 16/8/1.
//  版权   :  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLContactsViewController.h"
#import "XJLContactsTableViewCell.h"
#import "XJLContactHeaderView.h"
#import "XJLHandelBuddyRequestViewController.h"
#import "XJLDetailInfoViewController.h"
#import "XJLGroupListViewController.h"

@interface XJLContactsViewController()

/** 好友列表*/
@property(nonatomic,strong) NSMutableArray *buddyListM;

@end

@implementation XJLContactsViewController

-(NSMutableArray *)buddyListM{
    if (_buddyListM == nil) {
        _buddyListM = [NSMutableArray array];
    }
    return _buddyListM;
}

-(void)viewDidLoad{

    [super viewDidLoad];
    
    // 添加headerView
    [self setupHeaderView];
    
    //加载好友数据
    [self loadData];
}

// 添加headerView
-(void)setupHeaderView{
    
    XJLContactHeaderView *headerView = [XJLContactHeaderView headerView];
    // 先从本地获取未处理的好友请求数据
    NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];
    NSArray *buddyRequests = [defautls objectForKey:@"BuddyRequest"];
    
    headerView.redView.hidden = buddyRequests.count == 0;
    headerView.BtnClickBlock = ^(NSInteger idx){
        XJLLog(@"%ld",idx);
        if (idx == 0) {//进入处理“好友请求”界面
            XJLHandelBuddyRequestViewController *hbrVc = [[XJLHandelBuddyRequestViewController alloc]init];
            hbrVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:hbrVc animated:YES];            
        }else if (idx == 1){//群列表
            XJLGroupListViewController *groupListVc = [[XJLGroupListViewController alloc] init];
            groupListVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:groupListVc animated:YES];
        }
    };
    self.tableView.tableHeaderView = headerView;
}

-(void)loadData{
    //显示好友列表数据
    // 1.从本地获取好友列表
    
    NSArray *myBuddyList = [[EaseMob sharedInstance].chatManager buddyList];
    // 2.本地数据库没有好友列表数据,则从服务器获取
    if (myBuddyList.count == 0) {
        [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
            XJLLog(@"从服务器获取好友列表");
            // 服务器没有返回好友,直接return
            if (buddyList.count == 0) {
                return;
            }
            [self.buddyListM addObjectsFromArray:buddyList];
            
            [self.tableView reloadData];
            
        } onQueue:nil];
    }else{
        XJLLog(@"从本地数据获取好友列表");
        [self.buddyListM addObjectsFromArray:myBuddyList];
    }
    XJLLog(@"%@",myBuddyList);
    
}

#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.buddyListM.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *ID = @"ContactCell";
    XJLContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //获取好友对象
    EMBuddy *buddy = self.buddyListM[indexPath.row];
//    cell.textLabel.text = buddy.username;
    //显示用户名
    cell.buddyName.text = buddy.username;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //进入详情消息界面
    XJLDetailInfoViewController *detailInfoVc = [UIStoryboard storyboardWithName:@"XJLDetailInfoViewController" bundle:nil].instantiateInitialViewController;
    detailInfoVc.buddy = self.buddyListM[indexPath.row];
    detailInfoVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailInfoVc animated:YES];
    
}

@end
